/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.creatjobmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.creatjobmanage.entity.CreatJobProject;
import com.lczyfz.campusinfo.modules.creatjobmanage.service.CreatJobProjectService;

import java.util.List;

/**
 * 自带项目Controller
 * @author zms
 * @version 2018-03-25
 */
@Controller
@RequestMapping(value = "${adminPath}/creatjobmanage/creatJobProject")
public class CreatJobProjectController extends BaseController {

	@Autowired
	private CreatJobProjectService creatJobProjectService;
	@Autowired
	private SystemService systemService;

	@ModelAttribute
	public CreatJobProject get(@RequestParam(required=false) String id) {
		CreatJobProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = creatJobProjectService.get(id);
		}
		if (entity == null){
			entity = new CreatJobProject();
		}
		return entity;
	}
	
	@RequiresPermissions("creatjobmanage:creatJobProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(CreatJobProject creatJobProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CreatJobProject> page = creatJobProjectService.findPage(new Page<CreatJobProject>(request, response), creatJobProject);
		List<CreatJobProject> creatJobProjects = creatJobProjectService.findList(creatJobProject);
		model.addAttribute("creatJobProjects", creatJobProjects);
		model.addAttribute("page", page);
		return "modules/creatjobmanage/creatJobProjectList";
	}

	@RequiresPermissions("creatjobmanage:creatJobProject:view")
	@RequestMapping(value = "form")
	public String form(CreatJobProject creatJobProject, Model model) {
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("creatJobProject", creatJobProject);
		return "modules/creatjobmanage/creatJobProjectForm";
	}

	@RequiresPermissions("creatjobmanage:creatJobProject:edit")
	@RequestMapping(value = "save")
	public String save(CreatJobProject creatJobProject, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, creatJobProject)){
			return form(creatJobProject, model);
		}
		creatJobProjectService.save(creatJobProject);
		addMessage(redirectAttributes, "保存自带项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatJobProject/?repage";
	}
	
	@RequiresPermissions("creatjobmanage:creatJobProject:edit")
	@RequestMapping(value = "delete")
	public String delete(CreatJobProject creatJobProject, RedirectAttributes redirectAttributes) {
		creatJobProjectService.delete(creatJobProject);
		addMessage(redirectAttributes, "删除自带项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatJobProject/?repage";
	}
	@RequiresPermissions("creatjobmanage:creatJobProject:edit")
	@RequestMapping(value = "status")
	public String status(CreatJobProject creatJobProject, RedirectAttributes redirectAttributes) {
		creatJobProject.setEnrollStatus(CampusinfoOrderStat.DONE.getValue());
		creatJobProjectService.status(creatJobProject);
		addMessage(redirectAttributes, "审核自带项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatJobProject/?repage";
	}
}